/*
 * 系统项目名称：综合决策分析系统-version2.0
 * 文件名：ShopCompanyAction.java
 * 描述：
 * 版权：Copyright 2015-2020 义乌市跨境电商供应链管理有限公司-版权所有
 * 修改人：yangjx
 * 修改时间：2016-3-7 下午3:16:05
 * 版本号：V1.1
 */
package com.ywt.cda.action;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import com.alibaba.fastjson.JSONObject;
import com.ywt.cda.access.Access;
import com.ywt.cda.action.model.PortsModel;
import com.ywt.cda.domain.Ana2ndData;
import com.ywt.cda.enums.FunctionsEnum;
import com.ywt.cda.service.ParamValidateService;
import com.ywt.cda.service.PortService;
import com.ywt.cda.utils.DateUtils;

/**
 * （一句话功能简述） <br>
 * （功能详细描述）
 * 
 * @author yangjx
 * @version V1.0
 * @see [相关类/方法]
 * @since 2016-3-7 下午3:16:05
 */
@Controller
public class PortAction {

	private Logger logger = Logger.getLogger(PortAction.class);

	@Resource
	private ParamValidateService paramValidateService;
	@Resource
	private PortService portService;

	/**
	 * 出口-电商分析 getExportShopCompanyAnalyze(这里用一句话描述这个方法的作用) <br>
	 * (这里描述这个方法适用条件 – 可选)
	 * 
	 * @param portsModel
	 * @param model
	 * @return
	 * @exception
	 * @since 2016-3-11 下午1:57:55
	 * @author yangjx
	 */
	@RequestMapping("export/ports")
	@Access(FunctionsEnum.EXPORT_PORTS)
	public String getExportPortsAnalyze(
			@ModelAttribute("portsModel") PortsModel portsModel,
			Model model) 
	{
		if (null == portsModel) 
		{
			// 默认查近七天
			portsModel = new PortsModel();
			Date date = new Date();
			String today = DateUtils.format(date, "yyyy-MM-dd");
			portsModel.setStartTime(DateUtils.getSpecifiedDayBefore(
					today, 6));
			portsModel.setEndTime(today);
			portsModel.setCityOrPort("PORT");
			portsModel.setPageDatePattern("yyyy-mm-dd");
			model.addAttribute("default", true);
		}
		if (null != portsModel) 
		{
			if (StringUtils.isBlank(portsModel.getStartTime())
					&& StringUtils.isBlank(portsModel.getEndTime())) 
			{
				// 默认查近七天	
				Date date = new Date();
				String today = DateUtils.format(date, "yyyy-MM-dd");
				portsModel.setStartTime(DateUtils.getSpecifiedDayBefore(
						today, 6));
				portsModel.setEndTime(today);
				portsModel.setCityOrPort("PORT");
				portsModel.setPageDatePattern("yyyy-mm-dd");
				model.addAttribute("default", true);
			}
		}

		// 第一步：校验输入参数
		if (!paramValidateService.validate(portsModel)) 
		{
			logger.info("error param!");
			model.addAttribute("error", "error param!");
			return "error";
		}	
		portService.updateModel(portsModel);
		
		// 第二步：进行数据库查询	
		List<Ana2ndData> datas = portService.getExportData(
				portsModel);
		
		// 第三步：组装数据
		JSONObject result = new JSONObject();
		portService.toJson(datas, result,portsModel);
		logger.info(result.toJSONString());
		model.addAttribute("data", result.toJSONString());
		model.addAttribute("yes", portsModel.getSelectButton());
		return "export/ports";
	}
	
	@RequestMapping("import/ports")
	@Access(FunctionsEnum.IMPORT_PORTS)
	public String getImportPortsAnalyze(
			@ModelAttribute("portsModel") PortsModel portsModel,
			Model model) 
	{
		if (null == portsModel) 
		{
			// 默认查近七天
			portsModel = new PortsModel();
			Date date = new Date();
			String today = DateUtils.format(date, "yyyy-MM-dd");
			portsModel.setStartTime(DateUtils.getSpecifiedDayBefore(
					today, 6));
			portsModel.setEndTime(today);
			portsModel.setCityOrPort("PORT");
			portsModel.setPageDatePattern("yyyy-mm-dd");
			model.addAttribute("default", true);
		}
		if (null != portsModel) {
			if (StringUtils.isBlank(portsModel.getStartTime())
					&& StringUtils.isBlank(portsModel.getEndTime())) {
				// 默认查近七天
				Date date = new Date();
				String today = DateUtils.format(date, "yyyy-MM-dd");
				portsModel.setStartTime(DateUtils.getSpecifiedDayBefore(
						today, 6));
				portsModel.setEndTime(today);
				portsModel.setCityOrPort("PORT");
				portsModel.setPageDatePattern("yyyy-mm-dd");
				model.addAttribute("default", true);
			}
		}

		// 第一步：校验输入参数
		if (!paramValidateService.validate(portsModel)) 
		{
			logger.info("error param!");
			model.addAttribute("error", "error param!");
			return "error";
		}
		portService.updateModel(portsModel);
		
		// 第二步：进行数据库查询
		List<Ana2ndData> datas = portService.getImportData(
				portsModel);

		// 第三步：组装数据	
		JSONObject result = new JSONObject();
		portService.toJson(datas, result,portsModel);		
		logger.info(result.toJSONString());
		model.addAttribute("data", result.toJSONString());
		model.addAttribute("yes", portsModel.getSelectButton());
		return "import/ports";
	}
}
